package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;

import longcaisuyun.longcai.com.net.PostDdph;
import longcaisuyun.longcai.com.net.PostHaoPingPaiHangBang;
import longcaisuyun.longcai.com.suyunbean.DingDanPaiHangBang;
import longcaisuyun.longcai.com.suyunbean.HaoPingPaiHangBang;



public class SuYunActivity extends Activity implements View.OnClickListener {

    private static final int SUCCESS = 1;
    TextView tv_dingdan, tv_haoping, t_jieshao;
    TextView tv_name_wodepaiming, tv_name_dingdanshuliang, tv_name_shangbang;
    TextView tv_data_wodepaiming, tv_data_dingdanshuliang, tv_data_shangbang;
    TextView t_shangbang;
    ListView lv;
    //PaiHangAdapter p;
    LinearLayout fanhui_lin;
    DingDanPaiHangBang dingDanPaiHangBang;
    Handler myHandler = new Handler() {
        //2.重写消息处理函数
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //判断发送的消息
                case SUCCESS:
                    //更新View
                    DingDanPaiHangBang dingDanInfo = (DingDanPaiHangBang) msg.obj;
                    tv_data_wodepaiming.setText(dingDanInfo.getRank() + "");
                    tv_data_dingdanshuliang.setText(dingDanInfo.getNumber() + "");
                    if(dingDanInfo.getDifference()==null||dingDanInfo.getDifference().equals("null"))
                    {
                        tv_data_shangbang.setText("0");
                    }else
                    {
                        tv_data_shangbang.setText(dingDanInfo.getDifference() + "");
                    }

                    break;
            }
            super.handleMessage(msg);
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_su_yun);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
        DataInit();

    }


    private void DataInit() {
        new PostDdph(MyApplication.myPreferences.readUid(), new AsyCallBack<DingDanPaiHangBang>() {
            @Override
            public void onSuccess(String toast, int type, DingDanPaiHangBang dingDanInfo) throws Exception {
                super.onSuccess(toast, type, dingDanInfo);
                Message message = new Message();
                //发送消息与处理函数里一致
                message.what = SUCCESS;
                message.obj = dingDanInfo;
                //内部类调用外部类的变量
                myHandler.sendMessage(message);
                precessDingDanPaiHangData(dingDanInfo);
            }
            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(SuYunActivity.this, "网络故障，请稍后再试", Toast.LENGTH_SHORT).show();
            }
        }).execute(SuYunActivity.this);
    }

    private void ViewInit() {
        fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        t_shangbang= (TextView) findViewById(R.id.t_shangbang);
        tv_dingdan = (TextView) findViewById(R.id.tv_dingdan);
        tv_haoping = (TextView) findViewById(R.id.tv_haoping);
        tv_dingdan.setOnClickListener(this);
        tv_haoping.setOnClickListener(this);
        fanhui_lin.setOnClickListener(this);
        tv_name_wodepaiming = (TextView) findViewById(R.id.tv_name_wodepaiming);
        tv_name_dingdanshuliang = (TextView) findViewById(R.id.tv_name_dingdanshuliang);
        tv_name_shangbang = (TextView) findViewById(R.id.tv_name_shangbang);
        tv_data_wodepaiming = (TextView) findViewById(R.id.tv_data_wodepaiming);
        tv_data_dingdanshuliang = (TextView) findViewById(R.id.tv_data_dingdanshuliang);
        tv_data_shangbang = (TextView) findViewById(R.id.tv_data_shangbang);
        t_jieshao = (TextView) findViewById(R.id.t_jieshao);
        lv = (ListView) findViewById(R.id.listView);
        // lv.setAdapter(p);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_dingdan:
                switchtext(0);
                tv_name_dingdanshuliang.setText("订单数量");
                t_shangbang.setText("个订单");
                t_jieshao.setText("订单数量");
                new PostDdph(MyApplication.myPreferences.readUid(), new AsyCallBack<DingDanPaiHangBang>() {
                    @Override
                    public void onSuccess(String toast, int type, DingDanPaiHangBang dingDanInfo) throws Exception {
                        super.onSuccess(toast, type, dingDanInfo);
                        precessDingDanPaiHangData(dingDanInfo);
                        System.out.println("订单排行" + dingDanInfo.getRank());
                        Log.e("tv_paiming", dingDanInfo.getRank() + "");
                        tv_data_wodepaiming.setText(dingDanInfo.getRank() + "");
                        tv_data_dingdanshuliang.setText(dingDanInfo.getNumber() + "");
                        tv_data_shangbang.setText(dingDanInfo.getDifference() + "");

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }
                }).execute(SuYunActivity.this);
                break;

            case R.id.tv_haoping:
                switchtext(1);
                new PostHaoPingPaiHangBang(MyApplication.myPreferences.readUid(), new AsyCallBack<HaoPingPaiHangBang>() {
                    @Override
                    public void onSuccess(String toast, int type, HaoPingPaiHangBang haoPingInfo) throws Exception {
                        super.onSuccess(toast, type, haoPingInfo);
                        processHaoPingPaiHang(haoPingInfo);
                        tv_data_wodepaiming.setText(haoPingInfo.getRank() + "");
                        tv_data_dingdanshuliang.setText(haoPingInfo.getEvaluate() + "");
                        tv_data_shangbang.setText(haoPingInfo.getDifference() + "");
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }
                }).execute(SuYunActivity.this);
                tv_name_dingdanshuliang.setText("好评数量");
                //t_num.setText("好评数量");
                t_shangbang.setText("个好评");
                t_jieshao.setText("好评数量");
                break;

            case R.id.fanhui_lin:
                SuYunActivity.this.finish();
                break;
        }
    }

    private void processHaoPingPaiHang(HaoPingPaiHangBang haoPingInfo) {
        lv.setAdapter(new HaoPingAdapter(this, haoPingInfo));
    }

    private void precessDingDanPaiHangData(DingDanPaiHangBang dingDanInfo) {
        lv.setAdapter(new PaiHangAdapter(this, dingDanInfo));
    }
    public void switchtext(int j) {
        TextView[] text = {tv_dingdan, tv_haoping};
        for (int i = 0; i < text.length; i++) {
            if (i == j) {
                text[i].setTextColor(Color.parseColor("#ffffff"));
                text[i].setBackgroundColor(Color.parseColor("#e88837"));
            } else {
                text[i].setTextColor(Color.parseColor("#5f5f5f"));
                text[i].setBackgroundColor(Color.parseColor("#ffffff"));
            }
        }

    }


}
